<template>
  <div class="search">
    <div class="top">
      <div class="ipt">
        <input
          type="text"
          placeholder="最新菜谱"
          @keyup="getKeyfun(keylist)"
          @keydown.enter="getKeyFun(keylist)"
          v-model="keylist"
        />
        <div class="iconfont icon-sousuo"></div>
      </div>

      <div class="cancel" @click="$router.go(-1)">取消</div>
    </div>

    <div class="title" v-if="!keylist">热门搜索</div>

    <div v-if="!keylist">
      <div
        v-for="(item, index) in Search"
        :key="index"
        class="item"
        :style="{
          color: item.color,
          backgroundColor: item.background_color,
          borderColor: item.borderColor,
        }"
        @click="getkey(item.title)"
      >
        {{ item.title }}
      </div>
    </div>

    <div class="suggests_name" v-if="keylist">
      <div
        class="suggests_names"
        v-for="(item, index) in suggests"
        :key="index"  @click="getkey(item.name)"
      >
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
import { getSearch, getsuggests } from "../../api/search";
export default {
  data() {
    return {
      Search: null,
      suggests: null,
      keylist: "",
    };
  },
  methods: {
    getSearchFun() {
      getSearch().then((data) => {
        this.Search = data.result.suggestdetails;
        console.log(this.Search);
      });
    },
    getKeyfun(key) {
      console.log(key);
      getsuggests({ kw: key }).then((data) => {
        this.suggests = data.result.suggests;
        console.log(this.suggests);
      });
    },
    getKeyFun(key) {
      this.$router.push({
        path: "/Search/searchlist",
        query: { key: key },
      });
    },
    getkey(key) {
      this.$router.push({
        path: "/Search/searchlist",
        query: { key: key },
      });
    },
  },
  created() {
    this.getSearchFun();
  },
};
</script>

<style lang='less'>
.search {
  margin: 0 20px;
  .top {
    background: #fff;
    padding-bottom: 5px;

    padding-top: 30px;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .ipt {
      position: relative;

      width: 80%;
      input {
        width: 93%;
        background: #f7f7f7;
        height: 33px;
        border: none;
        color: #9d9d9d;
        font-size: 15px;
        padding-left: 35px;
      }
      .icon-sousuo {
        position: absolute;
        left: 12px;
        top: 7px;
        color: #9d9d9d;
        font-size: 18px;
      }
    }
    .cancel {
      font-weight: 12px;
      color: #232323;
      margin-left: 10px;
    }
  }
  .title {
    margin-top: 30px;
    margin-bottom: 16px;
    color: #262626;
    font-size: 14px;
    font-weight: bold;
  }
  .item {
    padding: 6px 10px;
    font-size: 12px;
    margin-right: 8px;
    margin-bottom: 10px;

    width: fit-content;
    border: 1px solid;

    border-radius: 5px;
    display: inline-block;
  }
  .suggests_name {
    margin-top: 20px;
    .suggests_names {
      padding-bottom: 10px;
      border-bottom: 3px solid #f6f6f6;
      margin-bottom: 13px;
    }
  }
}
</style>